﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Logger;
using Phoenix.ReflectionExtensions;

namespace PServiceBus.Core.Configuration {
    internal class MessageStoreElement : ConfigurationElement, IMessageStoreConfig {
        public MessageStoreElement() { }
        public MessageStoreElement(string name) { Name = name; }
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name {
            get { return base["name"] as string; }
            set { base["name"] = value; }
        }
        [ConfigurationProperty("address", IsRequired=false)]
        public string Address {
            get { return base["address"] as string; }
            set { base["address"] = value; }
        }
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }
        [ConfigurationProperty("enabled", IsRequired = false)]
        public bool Enabled {
            get { return (base["enabled"] as bool?) ?? false; }
            set { base["enabled"] = value; }
        }
    }
    [ConfigurationCollection(typeof(MessageStoreElement), CollectionType = ConfigurationElementCollectionType.BasicMap)]
    internal sealed class MessageStoreCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new MessageStoreElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return (element as MessageStoreElement).Name;
        }
    }
    public sealed class MessageStoreConfigSection : ConfigurationSection {
        [ConfigurationProperty("stores", IsDefaultCollection = true, IsRequired = true)]
        internal MessageStoreCollection Stores {
            get { return this["stores"] as MessageStoreCollection; }
        }
    }
}
